package com.zenithmind.version.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 仓库统计信息实体类 - 遵循单一职责原则，专门处理仓库统计数据
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("version_repository_statistics")
@Schema(description = "仓库统计信息实体")
public class RepositoryStatistics extends BaseEntity {

    /**
     * 仓库ID
     */
    @Schema(description = "仓库ID")
    private String repositoryId;

    /**
     * 仓库大小（字节）
     */
    @Schema(description = "仓库大小（字节）")
    private Long size;

    /**
     * 文件数量
     */
    @Schema(description = "文件数量")
    private Integer fileCount;

    /**
     * 提交数量
     */
    @Schema(description = "提交数量")
    private Integer commitCount;

    /**
     * 分支数量
     */
    @Schema(description = "分支数量")
    private Integer branchCount;

    /**
     * 标签数量
     */
    @Schema(description = "标签数量")
    private Integer tagCount;

    /**
     * 贡献者数量
     */
    @Schema(description = "贡献者数量")
    private Integer contributorCount;

    /**
     * 获取格式化的仓库大小
     * 
     * @return 格式化的大小字符串
     */
    public String getFormattedSize() {
        if (size == null || size == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double sizeValue = size.doubleValue();
        
        while (sizeValue >= 1024 && unitIndex < units.length - 1) {
            sizeValue /= 1024;
            unitIndex++;
        }
        
        return String.format("%.1f %s", sizeValue, units[unitIndex]);
    }
    
    /**
     * 检查是否为空仓库
     * 
     * @return 是否为空仓库
     */
    public boolean isEmpty() {
        return (fileCount == null || fileCount == 0) && 
               (commitCount == null || commitCount == 0);
    }
    
    /**
     * 获取总的代码对象数量
     * 
     * @return 总数量
     */
    public int getTotalObjects() {
        int total = 0;
        if (fileCount != null) total += fileCount;
        if (commitCount != null) total += commitCount;
        if (branchCount != null) total += branchCount;
        if (tagCount != null) total += tagCount;
        return total;
    }
}
